package com.quick.develop.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.quick.develop.framework.common.annotation.CreateForm;
import com.quick.develop.framework.common.annotation.DetailForm;
import com.quick.develop.framework.common.annotation.QueryTable;
import com.quick.develop.framework.common.annotation.UpdateForm;
import lombok.AllArgsConstructor;
import com.quick.develop.framework.common.utils.PageResult;
import com.quick.develop.framework.mybatis.service.impl.BaseServiceImpl;
import com.quick.develop.business.convert.EnvironmentalFactorIdentificationConvert;
import com.quick.develop.business.entity.EnvironmentalFactorIdentificationEntity;
import com.quick.develop.business.query.EnvironmentalFactorIdentificationQuery;
import com.quick.develop.business.vo.EnvironmentalFactorIdentificationVO;
import com.quick.develop.business.dao.EnvironmentalFactorIdentificationDao;
import com.quick.develop.business.service.EnvironmentalFactorIdentificationService;
import com.quick.develop.form.utils.ExcelUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 环境因素识别明细信息
 *
 * @author GJH 1906559468@qq.com
 * @since 1.0.0 2024-01-27
 */
@Service
@AllArgsConstructor
public class EnvironmentalFactorIdentificationServiceImpl extends BaseServiceImpl<EnvironmentalFactorIdentificationDao, EnvironmentalFactorIdentificationEntity> implements EnvironmentalFactorIdentificationService {

    @QueryTable
    @Override
    public PageResult<EnvironmentalFactorIdentificationVO> page(EnvironmentalFactorIdentificationQuery query) {
        IPage<EnvironmentalFactorIdentificationEntity> page = baseMapper.selectPage(getPage(query), getWrapperOptional(query));

        return new PageResult<>(EnvironmentalFactorIdentificationConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<EnvironmentalFactorIdentificationEntity> getWrapper(EnvironmentalFactorIdentificationQuery query) {
        LambdaQueryWrapper<EnvironmentalFactorIdentificationEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    private LambdaQueryWrapper<EnvironmentalFactorIdentificationEntity> getWrapperOptional(EnvironmentalFactorIdentificationQuery query) {
        LambdaQueryWrapper<EnvironmentalFactorIdentificationEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(!StrUtil.isBlankIfStr(query.getId()), EnvironmentalFactorIdentificationEntity::getId, query.getId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getBusinessKey()), EnvironmentalFactorIdentificationEntity::getBusinessKey, query.getBusinessKey());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getAttachId()), EnvironmentalFactorIdentificationEntity::getAttachId, query.getAttachId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getTargetId()), EnvironmentalFactorIdentificationEntity::getTargetId, query.getTargetId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getWorkshop()), EnvironmentalFactorIdentificationEntity::getWorkshop, query.getWorkshop());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getSubUnitSystem()), EnvironmentalFactorIdentificationEntity::getSubUnitSystem, query.getSubUnitSystem());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getSubUnitAction()), EnvironmentalFactorIdentificationEntity::getSubUnitAction, query.getSubUnitAction());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getEnvFactor()), EnvironmentalFactorIdentificationEntity::getEnvFactor, query.getEnvFactor());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getTense()), EnvironmentalFactorIdentificationEntity::getTense, query.getTense());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getStatus()), EnvironmentalFactorIdentificationEntity::getStatus, query.getStatus());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getEvnImpact()), EnvironmentalFactorIdentificationEntity::getEvnImpact, query.getEvnImpact());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getJudgmentMethodType()), EnvironmentalFactorIdentificationEntity::getJudgmentMethodType, query.getJudgmentMethodType());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getExpertJudgment()), EnvironmentalFactorIdentificationEntity::getExpertJudgment, query.getExpertJudgment());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getPossibility()), EnvironmentalFactorIdentificationEntity::getPossibility, query.getPossibility());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getSeverity()), EnvironmentalFactorIdentificationEntity::getSeverity, query.getSeverity());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getManagementUrgency()), EnvironmentalFactorIdentificationEntity::getManagementUrgency, query.getManagementUrgency());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getScore()), EnvironmentalFactorIdentificationEntity::getScore, query.getScore());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRiskDegree()), EnvironmentalFactorIdentificationEntity::getRiskDegree, query.getRiskDegree());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getImportantEvnFactor()), EnvironmentalFactorIdentificationEntity::getImportantEvnFactor, query.getImportantEvnFactor());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRiskStrategy()), EnvironmentalFactorIdentificationEntity::getRiskStrategy, query.getRiskStrategy());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getManageStrategy()), EnvironmentalFactorIdentificationEntity::getManageStrategy, query.getManageStrategy());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getIdentifyDepartment()), EnvironmentalFactorIdentificationEntity::getIdentifyDepartment, query.getIdentifyDepartment());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getIdentifyDate()), EnvironmentalFactorIdentificationEntity::getIdentifyDate, query.getIdentifyDate());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRemark()), EnvironmentalFactorIdentificationEntity::getRemark, query.getRemark());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserCode()), EnvironmentalFactorIdentificationEntity::getCreatedUserCode, query.getCreatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserName()), EnvironmentalFactorIdentificationEntity::getCreatedUserName, query.getCreatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedTime()), EnvironmentalFactorIdentificationEntity::getCreatedTime, query.getCreatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserCode()), EnvironmentalFactorIdentificationEntity::getUpdatedUserCode, query.getUpdatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserName()), EnvironmentalFactorIdentificationEntity::getUpdatedUserName, query.getUpdatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedTime()), EnvironmentalFactorIdentificationEntity::getUpdatedTime, query.getUpdatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getDelFlag()), EnvironmentalFactorIdentificationEntity::getDelFlag, query.getDelFlag());
            wrapper.like(!StrUtil.isBlankIfStr(query.getIdLike()), EnvironmentalFactorIdentificationEntity::getId, query.getIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getBusinessKeyLike()), EnvironmentalFactorIdentificationEntity::getBusinessKey, query.getBusinessKeyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getAttachIdLike()), EnvironmentalFactorIdentificationEntity::getAttachId, query.getAttachIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getTargetIdLike()), EnvironmentalFactorIdentificationEntity::getTargetId, query.getTargetIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getWorkshopLike()), EnvironmentalFactorIdentificationEntity::getWorkshop, query.getWorkshopLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getSubUnitSystemLike()), EnvironmentalFactorIdentificationEntity::getSubUnitSystem, query.getSubUnitSystemLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getSubUnitActionLike()), EnvironmentalFactorIdentificationEntity::getSubUnitAction, query.getSubUnitActionLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getEnvFactorLike()), EnvironmentalFactorIdentificationEntity::getEnvFactor, query.getEnvFactorLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getTenseLike()), EnvironmentalFactorIdentificationEntity::getTense, query.getTenseLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getStatusLike()), EnvironmentalFactorIdentificationEntity::getStatus, query.getStatusLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getEvnImpactLike()), EnvironmentalFactorIdentificationEntity::getEvnImpact, query.getEvnImpactLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getJudgmentMethodTypeLike()), EnvironmentalFactorIdentificationEntity::getJudgmentMethodType, query.getJudgmentMethodTypeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getExpertJudgmentLike()), EnvironmentalFactorIdentificationEntity::getExpertJudgment, query.getExpertJudgmentLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getPossibilityLike()), EnvironmentalFactorIdentificationEntity::getPossibility, query.getPossibilityLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getSeverityLike()), EnvironmentalFactorIdentificationEntity::getSeverity, query.getSeverityLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getManagementUrgencyLike()), EnvironmentalFactorIdentificationEntity::getManagementUrgency, query.getManagementUrgencyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getScoreLike()), EnvironmentalFactorIdentificationEntity::getScore, query.getScoreLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRiskDegreeLike()), EnvironmentalFactorIdentificationEntity::getRiskDegree, query.getRiskDegreeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getImportantEvnFactorLike()), EnvironmentalFactorIdentificationEntity::getImportantEvnFactor, query.getImportantEvnFactorLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRiskStrategyLike()), EnvironmentalFactorIdentificationEntity::getRiskStrategy, query.getRiskStrategyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getManageStrategyLike()), EnvironmentalFactorIdentificationEntity::getManageStrategy, query.getManageStrategyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getIdentifyDepartmentLike()), EnvironmentalFactorIdentificationEntity::getIdentifyDepartment, query.getIdentifyDepartmentLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getIdentifyDateLike()), EnvironmentalFactorIdentificationEntity::getIdentifyDate, query.getIdentifyDateLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRemarkLike()), EnvironmentalFactorIdentificationEntity::getRemark, query.getRemarkLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserCodeLike()), EnvironmentalFactorIdentificationEntity::getCreatedUserCode, query.getCreatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserNameLike()), EnvironmentalFactorIdentificationEntity::getCreatedUserName, query.getCreatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedTimeLike()), EnvironmentalFactorIdentificationEntity::getCreatedTime, query.getCreatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserCodeLike()), EnvironmentalFactorIdentificationEntity::getUpdatedUserCode, query.getUpdatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserNameLike()), EnvironmentalFactorIdentificationEntity::getUpdatedUserName, query.getUpdatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedTimeLike()), EnvironmentalFactorIdentificationEntity::getUpdatedTime, query.getUpdatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getDelFlagLike()), EnvironmentalFactorIdentificationEntity::getDelFlag, query.getDelFlagLike()).or();
        return wrapper;
    }

    @CreateForm
    @Override
    public void save(EnvironmentalFactorIdentificationVO vo) {
        EnvironmentalFactorIdentificationEntity entity = EnvironmentalFactorIdentificationConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @UpdateForm
    @Override
    public void update(EnvironmentalFactorIdentificationVO vo) {
        EnvironmentalFactorIdentificationEntity entity = EnvironmentalFactorIdentificationConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> idList) {
        removeByIds(idList);
    }

    @Override
    public EnvironmentalFactorIdentificationVO getEnvironmentalFactorIdentificationVO(String id) {
    EnvironmentalFactorIdentificationEntity entity = getById(id);
        return EnvironmentalFactorIdentificationConvert.INSTANCE.convert(entity);
    }

    @DetailForm
    @Override
    public EnvironmentalFactorIdentificationVO getEnvironmentalFactorIdentificationByBusinessKey(String businessKey) {
        EnvironmentalFactorIdentificationEntity entity = baseMapper.selectOne(new LambdaQueryWrapper<EnvironmentalFactorIdentificationEntity>()
                .eq(EnvironmentalFactorIdentificationEntity::getBusinessKey, businessKey));
        return EnvironmentalFactorIdentificationConvert.INSTANCE.convert(entity);
    }

    @Override
    public List<EnvironmentalFactorIdentificationVO> getListByKeys(EnvironmentalFactorIdentificationQuery query) {
        return this.getListByKeys(query.getFieldName(), query.getCodes());
    }

    @Override
    public void dynamicExportExcel(EnvironmentalFactorIdentificationQuery query) {
        List<EnvironmentalFactorIdentificationEntity> list = this.list(getWrapperOptional(query));
        ExcelUtils.downloadExcel(EnvironmentalFactorIdentificationEntity.class, list, "环境因素识别明细信息导出");
    }

    @Override
    public void dynamicImportExcel(MultipartFile file) {
        ExcelUtils.execWriteDatabase(
                file,
                EnvironmentalFactorIdentificationEntity.class,
                this::filterDynamicImport,
                this::overrideDynamicImport,
                this::updateBatchById,
                this::saveBatch
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSave(List<EnvironmentalFactorIdentificationVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalFactorIdentificationEntity> entities = list.stream().map(EnvironmentalFactorIdentificationConvert.INSTANCE::convert).collect(Collectors.toList());
            saveBatch(entities);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchUpdateById(List<EnvironmentalFactorIdentificationVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalFactorIdentificationEntity> entities = list.stream().map(EnvironmentalFactorIdentificationConvert.INSTANCE::convert).collect(Collectors.toList());
            updateBatchById(entities);
        }
    }

    public List<EnvironmentalFactorIdentificationVO> getListByKeys(String key, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys) && StrUtil.isNotEmpty(key)) {
            return Collections.emptyList();
        }
        return EnvironmentalFactorIdentificationConvert.INSTANCE.convertList(list(Wrappers.query(EnvironmentalFactorIdentificationEntity.class).in(key, keys)));
    }

    /**
    *
    * @param sFunction 函数式获取Key
    * @param keys 需要查询的列表
    * @return 返回数据列表
    */
    public List<EnvironmentalFactorIdentificationEntity> getListByKeys(SFunction<EnvironmentalFactorIdentificationEntity, ?> sFunction, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys)) {
            return Collections.emptyList();
        }
        return list(Wrappers.lambdaQuery(EnvironmentalFactorIdentificationEntity.class).in(sFunction, keys));
    }

    /**
     * @param list 解析的数据
     * @return 过滤以后的数据
     */
    public List<EnvironmentalFactorIdentificationEntity> filterDynamicImport(List<EnvironmentalFactorIdentificationEntity> list) {
        return list;
    }

    /**
     * @param list 解析的数据
     * @return 返回过滤的数据
     * @description 覆盖导入的数据
     */
    public List<EnvironmentalFactorIdentificationEntity> overrideDynamicImport(List<EnvironmentalFactorIdentificationEntity> list) {
        return Collections.emptyList();
    }
}